//
//  RKAuthenticationExample.m
//  ZooNgonDoiOS
//
//  Created by Trung sound on 6/13/12.
//  Copyright (c) 2012 Personal. All rights reserved.
//

#import "RKAuthenticationExample.h"
#import "ZooNetwork.h"
#import "ZooUtil.h"
@implementation RKAuthenticationExample

@synthesize authenticatedRequest;
@synthesize URLTextField;
@synthesize usernameTextField;
@synthesize passwordTextField;
@synthesize btnCheck;
@synthesize authenticationTypePickerView;

#define CONSUMER_KEY  @"0a9fd3a81e14b1ca6e9a60ba6751488878c66737";
#define CONSUMER_SECRET  @"99e179f81ae8c8613ef840e0d36d68f3c9fab11b";

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

//        RKClient *client = [[RKClient alloc] initWithBaseURLString:@"http://api.ngon.do"];
//        client.OAuth1ConsumerKey = CONSUMER_KEY;
//        client.OAuth1ConsumerSecret = CONSUMER_SECRET;
//        client.authenticationType = RKRequestAuthenticationTypeOAuth1;
//        [RKClient setSharedClient:client];

        RKObjectManager *objectManager = [RKObjectManager managerWithBaseURLString:@"http://api.ngon.do"];
        
        objectManager.client.OAuth1ConsumerKey = CONSUMER_KEY;
        objectManager.client.OAuth1ConsumerSecret = CONSUMER_SECRET;
        objectManager.client.authenticationType = RKRequestAuthenticationTypeOAuth1;
        
        [RKObjectManager setSharedManager:objectManager];
        
        URLTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 300, 50)];
        URLTextField.backgroundColor = [UIColor whiteColor];
        URLTextField.placeholder = @"URL";
        
        usernameTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 70, 150, 30)];
        usernameTextField.backgroundColor = [UIColor whiteColor];
        usernameTextField.placeholder = @"Username";
        
        passwordTextField = [[UITextField alloc]initWithFrame:CGRectMake(170, 70, 150, 30)];
        passwordTextField.backgroundColor = [UIColor whiteColor];
        passwordTextField.placeholder = @"password";
        
        btnCheck = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btnCheck.frame = CGRectMake(50, 110, 60, 30);
        [btnCheck setTitle:@"CHECK" forState:UIControlStateNormal];
        [btnCheck addTarget:self action:@selector(sendRequest) forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:URLTextField];
        [self.view addSubview:usernameTextField];
        [self.view addSubview:passwordTextField];
        [self.view addSubview:btnCheck];
        
        
    }
    return self;
}

/**
 We are constructing our own RKRequest here rather than working with the client.
 It is important to remember that RKClient is really just a factory object for instances
 of RKRequest. At any time you can directly configure an RKRequest instead.
 */
- (void)sendRequest {
    [[ZooNetwork sharedNetwork] sendRequestOAuth];

//    NSURL *URL = [NSURL URLWithString:[URLTextField text]];
    //    RKRequest *newRequest = [RKRequest requestWithURL:URL];
    
//    NSDictionary *params = [NSDictionary dictionaryWithObject:@"ngondo" forKey:@"Sender"];
//    
//    [[RKClient sharedClient] post:@"/auth/request_token" params:params delegate:self];
    
//    [[RKClient sharedClient] get:@"/auth/request_token" delegate:self];
    
//    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/auth/request_token" delegate:self];
}

- (void)request:(RKRequest *)request didFailLoadWithError:(NSError *)error {
    RKLogError(@"Load of RKRequest %@ failed with error: %@", request, error);
    [request release];
}

- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {
//    RKLogCritical(@"Loading of RKRequest %@ completed with status code %d. Response body: %@", request, response.statusCode, [response bodyAsString]);
    
    if ([request isGET]) {
        if ([response isOK]) {
            NSString *myJson = [response bodyAsString];
            NSLog(@"my json: %@", myJson);
            
            
        }
    }
    NSLog(@"response code: %d", [response statusCode]);
}

- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObject:(id)object {
    NSLog(@"OK");
}

- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error {
    NSLog(@"%@", error);
}

//- (BOOL)isNetworkReachable {
//    if ([[[UIDevice currentDevice] model] isEqualToString:@"iPad Simulator"] || [[[UIDevice currentDevice] model] isEqualToString:@"iPhone Simulator"])
//        return YES;
//    
//    [self validateIntrospection];
//    BOOL reachable = RKReachabilityNotReachable != [self networkStatus];
//    RKLogDebug(@"Reachability observer %@ determined isNetworkReachable = %d", self, reachable);
//    return reachable;
//}

#pragma mark - UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

    // returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return 0;
}


- (void)dealloc {
    [authenticatedRequest cancel];
    [authenticatedRequest release];
    authenticatedRequest = nil;
    
    [super dealloc];
}
@end
